package com.xyoto.community.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

/**
 * 寻氧O2对象存储服务
 */
@Service
public class XYOtoOssUtils implements XYOtoOss {

    private static String endpoint = "oss-cn-beijing.aliyuncs.com";
    private static String accessKeyId = "LTAI5tDS2wNnfdhbNTjKznyv";
    private static String accessKeySecret = "AKvCXhQLoonczR69NJcbD5MBVWAxet";
    private static String bucketName = "xyoto-test";

    /**
     * 批量上传图片
     * @param files 多图
     * @param mouldName 模块名称
     * @return
     * @throws IOException
     */
    @Override
    public String uploadFileAvatars(MultipartFile[] files, String mouldName) throws IOException {

        if (files == null || files.length == 0){
            return null;
        }

        try {
            //创建OSS实例
            OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

            StringBuilder buff = new StringBuilder();
            for (MultipartFile file : files) {
                //获取上传文件输入流
                InputStream inputStream = file.getInputStream();
                //获取文件名称
                String fileName = file.getOriginalFilename();

                //在文件名称中添加随机唯一的值
                String uuid = UUID.randomUUID().toString().replace("-", "");
                fileName  = new DateTime().toString("yyyy-MM-dd HH:mm:ss")+ " "+ uuid + fileName;

                String datePath = new DateTime().toString("yyyy/MM/dd");
                fileName = mouldName + "/" + datePath + "/" + fileName;

                ossClient.putObject(bucketName, fileName, inputStream);

               // String url = "https://" + "static.ityoto.com" + "/" + fileName;
                String url = "/" + fileName;

                buff.append(url);
                buff.append(",");
            }
            ossClient.shutdown();
            return buff.substring(0, buff.length() - 1);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 上传图片
     * @param file
     * @param mouldName
     * @return
     */
    @Override
    public String uploadFileAvatar(MultipartFile file, String mouldName) throws IOException {

        if (file == null) return null;

        try {
            //创建OSS实例
            OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

            //获取上传文件输入流
            InputStream inputStream = file.getInputStream();
            //获取文件名称
            String fileName = file.getOriginalFilename();

            //在文件名称中添加随机唯一的值
            String uuid = UUID.randomUUID().toString().replace("-", "");
            fileName  = new DateTime().toString("yyyy-MM-dd HH:mm:ss")+ " "+ uuid + fileName;

            String datePath = new DateTime().toString("yyyy/MM/dd");
            fileName = mouldName + "/" + datePath + "/" + fileName;

            ossClient.putObject(bucketName, fileName, inputStream);

          //  String url = "https://" + "static.ityoto.com" + "/" + fileName;
            String url = "/" + fileName;

            ossClient.shutdown();

            return url;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }

    }

    /**
     * 删除文件
     * @param url 文件路径
     * @return
     */
    @Override
    public boolean deleteFileAvatar(String url){

        url = PictureUtils.addPrefix(url);

        if (url == null){
            return false;
        }
        String replace = url.replace("https://" + "/", "");
        try {
            OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
            ossClient.deleteObject(bucketName,replace);
            ossClient.shutdown();
            return true;

        }catch (Exception e){
            e.printStackTrace();
            return false;
        }

    }

}
